package com.example.nacos.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 日志工具类，提供统一的日志记录方式
 * 支持业务日志、异常日志的规范记录
 */
public class LogUtils {

    private static final Logger BUSINESS_LOGGER = LoggerFactory.getLogger("com.example.nacos.service");
    private static final Logger EXCEPTION_LOGGER = LoggerFactory.getLogger("com.example.nacos");
    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");

    /**
     * 生成日志跟踪ID
     * @return 唯一的跟踪ID
     */
    public static String generateTraceId() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * 记录业务日志
     * @param module 模块名称
     * @param operation 操作类型
     * @param userId 用户ID
     * @param params 操作参数
     * @param result 操作结果
     */
    public static void logBusiness(String module, String operation, String userId, Map<String, Object> params, String result) {
        Map<String, Object> logData = new HashMap<>();
        logData.put("timestamp", LocalDateTime.now().format(DATE_TIME_FORMATTER));
        logData.put("traceId", generateTraceId());
        logData.put("module", module);
        logData.put("operation", operation);
        logData.put("userId", userId);
        logData.put("params", params != null ? params : new HashMap<>());
        logData.put("result", result);
        
        BUSINESS_LOGGER.info("BUSINESS_LOG: {}", logData);
    }

    /**
     * 记录业务日志（简化版）
     * @param module 模块名称
     * @param operation 操作类型
     * @param message 日志消息
     */
    public static void logBusiness(String module, String operation, String message) {
        Map<String, Object> logData = new HashMap<>();
        logData.put("timestamp", LocalDateTime.now().format(DATE_TIME_FORMATTER));
        logData.put("traceId", generateTraceId());
        logData.put("module", module);
        logData.put("operation", operation);
        logData.put("message", message);
        
        BUSINESS_LOGGER.info("BUSINESS_LOG: {}", logData);
    }

    /**
     * 记录异常日志
     * @param module 模块名称
     * @param operation 操作类型
     * @param userId 用户ID
     * @param e 异常对象
     */
    public static void logException(String module, String operation, String userId, Exception e) {
        Map<String, Object> logData = new HashMap<>();
        logData.put("timestamp", LocalDateTime.now().format(DATE_TIME_FORMATTER));
        logData.put("traceId", generateTraceId());
        logData.put("module", module);
        logData.put("operation", operation);
        logData.put("userId", userId);
        logData.put("exceptionType", e.getClass().getName());
        logData.put("exceptionMessage", e.getMessage());
        
        EXCEPTION_LOGGER.error("EXCEPTION_LOG: {}", logData, e);
    }

    /**
     * 记录异常日志（简化版）
     * @param message 错误消息
     * @param e 异常对象
     */
    public static void logException(String message, Exception e) {
        EXCEPTION_LOGGER.error("EXCEPTION_LOG: {}, timestamp: {}", message, 
                LocalDateTime.now().format(DATE_TIME_FORMATTER), e);
    }

    /**
     * 记录警告日志
     * @param module 模块名称
     * @param message 警告消息
     */
    public static void logWarning(String module, String message) {
        BUSINESS_LOGGER.warn("WARNING_LOG: module={}, message={}, timestamp={}", 
                module, message, LocalDateTime.now().format(DATE_TIME_FORMATTER));
    }

    /**
     * 创建类特定的日志记录器
     * @param clazz 目标类
     * @return Logger实例
     */
    public static Logger getLogger(Class<?> clazz) {
        return LoggerFactory.getLogger(clazz);
    }

}